let fs = require('fs');
let http = require('http');
let nunjucks = require('nunjucks');
let mysql = require('mysql');
const { resolve } = require('path');
const { rejects } = require('assert');


let server = http.createServer();
server.on('request',function(req,res){
    
 console.log(req.url);
 if(req.url == '/index.html'){



      let connection = mysql.createConnection({host:"localhost",user:'root',password:'root',port:'3306',database:'yiqing'})
      connection.query(`select * from information`,function(err,rows){
            if(err){
               reject(err);
            }else{
               let html=''
             for(let row in rows){
             html+='<tr class="list_info" >'
             html+='<td class="haha" >'+ rows[row].city+'</td>'
             html+='<td>'+ rows[row].add+'</td>'
             html+='<td>'+ rows[row].all+'</td>'
             html+='<td>'+ rows[row].heathy+'</td>'
             html+='<td>'+ rows[row].dead+'</td>'
             html+='</tr>'
      }         
         console.log(html);
         fs.readFile('index.html',function(err,buffer){
            if(err){
               console.log(err);
            }else{
             let outPutStr = buffer.toString();
               //替换掉{{result}}标签
              let newStr= outPutStr.replace('{{result}}',html);
              res.write(newStr);
              res.end();

            }
         })

            }
      })

   }
})
server.listen(6060,function(){
     console.log("ok");
})